package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.domain.User;
import com.woniuxy.dto.DtoMyPage;
import com.woniuxy.dto.DtoUserInfo;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.parameter.UserLoginParam;
import com.woniuxy.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XXX
 * @since 2021-05-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean updateUsr(int userId, String account, String userName, String sex, String tel, String eMail) {
        return userMapper.updateUsr(userId, account, userName, sex, tel, eMail);
    }

    @Override //查询用户返回信息
    public User login(UserLoginParam userLoginParam) {
        User user = new User();
        BeanUtils.copyProperties(userLoginParam,user);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account", user.getAccount());
        wrapper.eq("password", user.getPassword());
        User user1 = userMapper.selectOne(wrapper);
        return user1;
    }

    @Override
    public DtoMyPage<DtoUserInfo> selectAllUsers(int current, int size) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Page<User> pages = new Page<>(current, size);
        IPage<User> iPage = userMapper.selectPage(pages, null);
        ArrayList<DtoUserInfo> arrayList = new ArrayList<>();
        for (User user : iPage.getRecords()) {
            DtoUserInfo dtoUserInfo = new DtoUserInfo();
            PropertyUtils.copyProperties(dtoUserInfo, user);
            arrayList.add(dtoUserInfo);
        }
        DtoMyPage<DtoUserInfo> dtoMyPage = new DtoMyPage<>();
        dtoMyPage.setRecords(arrayList);
        dtoMyPage.setTotal(iPage.getTotal());

        return dtoMyPage;
    }

    @Override
    public DtoMyPage<DtoUserInfo> selectUsrByInfo(String info, int current, int size) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.and(
                wrapper1 ->
                        wrapper1.like("user_name", info).or().like("account", info));
        Page<User> pages = new Page<>(current, size);
        IPage<User> iPage = userMapper.selectPage(pages, wrapper);
        ArrayList<DtoUserInfo> arrayList = new ArrayList<>();
        for (User user : iPage.getRecords()) {
            DtoUserInfo dtoUserInfo = new DtoUserInfo();
            PropertyUtils.copyProperties(dtoUserInfo, user);
            arrayList.add(dtoUserInfo);
        }
        DtoMyPage<DtoUserInfo> dtoMyPage = new DtoMyPage<>();
        dtoMyPage.setRecords(arrayList);
        dtoMyPage.setTotal(iPage.getTotal());

        return dtoMyPage;
    }
}
